<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="产品名称" prop="pName">
      <el-input v-model="dataForm.pName" placeholder="产品名称"></el-input>
    </el-form-item>
    <el-form-item label="单位规格" prop="pUnitName">
      <el-input v-model="dataForm.pUnitName" placeholder="单位规格"></el-input>
    </el-form-item>
    <el-form-item label="订单数量" prop="pNum">
      <el-input v-model="dataForm.pNum" placeholder="订单数量"></el-input>
    </el-form-item>
    <el-form-item label="产品单价" prop="pUnitPrice">
      <el-input v-model="dataForm.pUnitPrice" placeholder="产品单价"></el-input>
    </el-form-item>
    <el-form-item label="产品总价" prop="pTotalPrice">
      <el-input v-model="dataForm.pTotalPrice" placeholder="产品总价"></el-input>
    </el-form-item>
    <el-form-item label="Order id 主键" prop="parentId">
      <el-input v-model="dataForm.parentId" placeholder="Order id 主键"></el-input>
    </el-form-item>
    <el-form-item label="产品分类" prop="cid">
      <el-input v-model="dataForm.cid" placeholder="产品分类"></el-input>
    </el-form-item>
    <el-form-item label="产品id" prop="sid">
      <el-input v-model="dataForm.sid" placeholder="产品id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          pName: '',
          pUnitName: '',
          pNum: '',
          pUnitPrice: '',
          pTotalPrice: '',
          parentId: '',
          cid: '',
          sid: ''
        },
        dataRule: {
          pName: [
            { required: true, message: '产品名称不能为空', trigger: 'blur' }
          ],
          pUnitName: [
            { required: true, message: '单位规格不能为空', trigger: 'blur' }
          ],
          pNum: [
            { required: true, message: '订单数量不能为空', trigger: 'blur' }
          ],
          pUnitPrice: [
            { required: true, message: '产品单价不能为空', trigger: 'blur' }
          ],
          pTotalPrice: [
            { required: true, message: '产品总价不能为空', trigger: 'blur' }
          ],
          parentId: [
            { required: true, message: 'Order id 主键不能为空', trigger: 'blur' }
          ],
          cid: [
            { required: true, message: '产品分类不能为空', trigger: 'blur' }
          ],
          sid: [
            { required: true, message: '产品id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/payment/ordersubjoin/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.pName = data.orderSubjoin.pName
                this.dataForm.pUnitName = data.orderSubjoin.pUnitName
                this.dataForm.pNum = data.orderSubjoin.pNum
                this.dataForm.pUnitPrice = data.orderSubjoin.pUnitPrice
                this.dataForm.pTotalPrice = data.orderSubjoin.pTotalPrice
                this.dataForm.parentId = data.orderSubjoin.parentId
                this.dataForm.cid = data.orderSubjoin.cid
                this.dataForm.sid = data.orderSubjoin.sid
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/payment/ordersubjoin/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'pName': this.dataForm.pName,
                'pUnitName': this.dataForm.pUnitName,
                'pNum': this.dataForm.pNum,
                'pUnitPrice': this.dataForm.pUnitPrice,
                'pTotalPrice': this.dataForm.pTotalPrice,
                'parentId': this.dataForm.parentId,
                'cid': this.dataForm.cid,
                'sid': this.dataForm.sid
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
